﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;

namespace UserControlLib.Converters
{
    /// <summary>
    /// 索引转换器
    /// </summary>
    public class IndexToIntegerConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is ContentPresenter contentPresenter && contentPresenter.Parent is ItemsControl itemsControl)
            {
                var index = itemsControl.ItemContainerGenerator.IndexFromContainer(contentPresenter);
                return index;
            }
            return -1; // 默认值，表示无效索引
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
